import pygame
from pygame.locals import *
import random
from sys import exit
import time

#定义猴子类
class Monkey(object):
    def __init__(self,screen,font):
        self.x=300
        self.y=350
        self.screen=screen
        self.image=pygame.image.load("./resources/monkey.png")
        self.number=0
        self.fall_num=0
        self.font=font
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
    def move_left(self):
        self.x-=50
    def move_right(self):
        self.x+=50
    def move_up(self):
        self.y-=40
    def move_down(self):
        self.y+=40
    #判断人物是否接住水果
    def size(self):
        for apple in Fruit_fall.fruit_list:
            if apple.y>=self.y-20:
                if apple.x in range(self.x-40,self.x+41):
                    Fruit_fall.fruit_list.remove(apple)
                    self.number+=1
            #如果有水果没接住，游戏结束
            if apple.y>500:
                self.fall_num+=1
                if self.fall_num>=1:
                    text_surface = self.font.render("Game Over!!!!", True, (0, 0, 255), (0, 0, 0))
                    quit(self.screen,text_surface)

#定义水果父类
class Fruit(object):
    def __init__(self,x,screen,image):
        self.x=x
        self.y=0
        self.screen=screen
        self.image=pygame.image.load(image)
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
    def move(self):
        self.y+=0.15

#水果子类
class Apple(Fruit):
    pass
class Apple2(Fruit):
    pass
class Banana(Fruit):
    pass

#管理水果类
class Fruit_fall:
    fruit_list = []
    fruit = []
    def __init__(self, screen):
        self.screen = screen

    #创建水果对象
    def many(self):
        num = random.randint(1, 500)
        if num == 1 :
            x = random.randint(0, 650)
            apple = Apple(x, self.screen,'./resources/apple.png')
            apple2=Apple2(x,self.screen,'./resources/apple2.png')
            banana = Banana(x, self.screen, './resources/banana.png')
            Fruit_fall.fruit.append(apple)
            Fruit_fall.fruit.append(apple2)
            Fruit_fall.fruit.append(banana)
            fruit=random.choice(Fruit_fall.fruit)
            Fruit_fall.fruit_list.append(fruit)

    #显示水果
    def fall(self):
        self.many()
        for fruit in Fruit_fall.fruit_list:
            fruit.display()
            fruit.move()

#主程序
def start():
    # 查看系统可用字体
    # print(pygame.font.get_fonts())
    #初始化pygame
    pygame.init()
    # 设置字体，大小
    font = pygame.font.SysFont('arial', 50)
    #创建一个窗口
    screen=pygame.display.set_mode((700,500))
    #设置窗口标题
    pygame.display.set_caption("猴子接水果小游戏")
    #背景
    background=pygame.image.load("./resources/background.jpg")
    #创建猴子对象
    monkey=Monkey(screen,font)
    fruit=Fruit_fall(screen)
    while True:
        screen.blit(background,(0,0))
        #将分数添加至窗口中
        score_surface = font.render(str(monkey.number), True, (255, 0, 255))
        screen.blit(score_surface, (620, 10))
        #猴子
        monkey.display()
        monkey.size()
        event(monkey)
        #水果
        fruit.fall()
        pygame.display.update()
        #判断游戏时间
        t = pygame.time.get_ticks()
        # 判断游戏结束 ，超过60秒自动结束
        if t >= 60000:
            if monkey.number >= 20:
                # 设定文本内容，字体，背景颜色
                text_surface = font.render("Good!!!!!!!!", True, (0, 0, 255), (0, 0, 0))
                quit(screen, text_surface)
            else:
                text_surface = font.render("Game Over!!!!", True, (0, 0, 255), (0, 0, 0))
                quit(screen, text_surface)


#定义游戏退出方法
def quit(screen,text_surface):
    screen.blit(text_surface, (250, 270))
    pygame.display.update()
    time.sleep(3)
    pygame.quit()
    exit()


#键盘监听操作
def event(x):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                #人物左移
                x.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                #人物右移
                x.move_right()
            elif event.key == K_UP:
                # 人物上移
                x.move_up()
            elif event.key == K_DOWN:
                # 人物下移
                x.move_down()


if __name__=='__main__':
    start()